<?php
set_time_limit(0);
require('phpQuery/phpQuery.php');
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $link);
mysql_query('SET NAMES UTF8');

$date = !empty($_GET['date']) ? trim($_GET['date']) : '2015-06-15';
$year = substr($date, 0, -3);
$day = substr($date, -2);

echo '正在采集'.$date;
$result = phpQuery::newDocumentFile('http://zqrb.ccstock.cn/html/'.$year.'/'.$day.'/node_2.htm');
$result = $result->find('span.STYLE1')->filter(':contains(信息披露)')->parent()->parent()->parent()->next()->find('.vote_content12px');
preg_match_all('/href=\"(.*?)\"/', $result, $match);

foreach ($match[1] as $key => $value) {
	$url = 'http://zqrb.ccstock.cn/html/'.$year.'/'.$day.'/'.$value;
	$result2 = phpQuery::newDocumentFile($url);
	$title = trim($result2->find('.h1')->text());
	$content = $result2->find('founder-content')->text();
	preg_match('/证券代码：(\d{6})/', $content, $match2);
	preg_match("/证券简称：(.*?)公告编号/", $content, $match3);
	//$code = preg_match('/证券代码：(\d{6})/', $content, $match4);
	//print_r($match2);
	//print_r($match3);
	$code = !empty($match2[1]) ? trim($match2[1]) : '';
	$name = !empty($match3[1]) ? trim($match3[1]) : '';
	$sql = "INSERT INTO info VALUES (null, '{$title}', '{$code}', '{$name}', '', '{$content}', '{$date}', '{$url}')";
	mysql_query($sql);
}

if($date < date('Y-m-d')){
	$date = date('Y-m-d', strtotime($date)+86400);
	echo '<meta http-equiv="Refresh" content="2;url=?date='.$date.'">';
}else{
	echo '采集完成';
}
